###################################################################################
#####   导入库
###################################################################################
import os
import numpy
###################################################################################
#####   指定路径
###################################################################################
import os

os.chdir(os.path.dirname(__file__)) 
# folder_path = "D:\\Firmware\\Python_Project\\SHS_dynamic_range\\test"
###################################################################################
#####   参数
###################################################################################
Width = 3072
Height = 3072
###################################################################################
#####   算法
###################################################################################
def get_dynamic_range(path,file,Width,Height) :
    if (os.path.splitext(file)[1] == ".raw") :
        raw = numpy.fromfile(path,dtype='uint16')
        raw = raw.reshape(Height,Width,1)
        row_min = []
        j = 0
        while j < Width :
            col_list = raw[:,j]
            col_list = col_list[col_list != 0]
            col_min  = numpy.min(col_list)
            row_min = numpy.append(row_min,col_min)
            j = j + 1 
        # print(row_min)
        # 排查每个DAC通道里的异常点并去除
        row_new = []
        i = 0 
        while i < (Width/64) : 
            # 异常值判断条件，这里以超过一个标准差为异常值为例
            pix_list = row_min[i*64:i*64+64]
            #方差去除异值
            # pix_mean = numpy.mean(pix_list)
            # pix_std = numpy.std(pix_list)
            # pix_list = pix_list[((pix_mean - 3*pix_std) <= pix_list) & (pix_list <= (pix_mean + 3*pix_std))]
            #去几个极大极小值去除异值
            pix_list = numpy.sort(pix_list)
            pix_list = pix_list[1:62]
            #去除异常数据后的新数据
            row_new = numpy.append(row_new,pix_list)
            i = i + 1
        #取最大最小值
        pix_max  = numpy.max(row_new)
        pix_min  = numpy.min(row_new)
        range    = int(pix_max - pix_min)
        dynamic_range = (65536-range) / 65536 * 100
        dynamic_range = f"{dynamic_range:.2f}%"
        # print(pix_min,pix_max,len(row_new),dynamic_range)
        return dynamic_range
    else :
        return 
# result = get_dynamic_range(os.path.join(folder_path,"389.raw"),"389.raw",Width,Height)    #测试定义函数
# print(result)
###################################################################################
#####   处理
###################################################################################
for file0 in os.listdir(r'.') :
    path0 = os.path.join(r'.', file0)
    print(path0)
    if os.path.isdir(path0) :
        for file1 in os.listdir(path0) :
            path1 = os.path.join(path0, file1)
            print(path1)
            if os.path.isdir(path1) :
                file2_num = 0
                for file2 in os.listdir(path1) :
                    path2 = os.path.join(path1, file2)
                    if os.path.isdir(path2) :
                        print("深度不支持")
                    else :
                        result = get_dynamic_range(path2,file2,Width,Height)
                        if result : print(file0,file1,file2,result)
                        file2_num = file2_num + 1
            else :
                result = get_dynamic_range(path1,file1,Width,Height)
                if result : print(file0,file1," ",result)
    else :
        result = get_dynamic_range(path0,file0,Width,Height)
        if result : print(file0," "," ",result)